﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows.Input;
using VisaCRM.Modules.Admin.Model;
using VisaCRM.Modules.Admin.Gateway;
using VisaCRM.Modules.Admin.ViewModels.Helper;
using Microsoft.Practices.Prism.Mvvm;

namespace VisaCRM.Modules.Admin.ViewModels
{
    /// <summary>
    /// ViewModel of an list of titles
    /// </summary>
    class TitleViewModel:BindableBase
    {
        /// <summary>
        /// initialises all collection used
        /// </summary>
        public ObservableCollection<Title> allTitles;
        public ObservableCollection<string> AllGender { get; set; }

        /// <summary>
        /// initialise gateway used
        /// </summary>
        TitleGateway titleGateway = new TitleGateway();

        ///summary
        ///initialises a new instance of TitleViewModel class
       ///summary
        public TitleViewModel(ObservableCollection<Title> alltitles)
       {
           currentTitle = new Title();
           if (alltitles == null || alltitles.Count == 0)
           {

               AllTitles = titleGateway.GetAllTitles();
           }
           else
           {
               this.AllTitles = alltitles;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllTitles.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentTitle))
               {
                   this.CurrentTitle = null;
               }
           };

           this.AllGender = titleGateway.GetAllGender();
 
           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.DeleteCommand = new DelegateCommand((o) => this.Delete());
           this.EditCommand = new DelegateCommand((o) => this.Edit());
       }

       public ICommand AddNewCommand { get; private set; }
       public ICommand SaveCommand { get; private set; }
       public ICommand CancelCommand { get; private set; }
       public ICommand DeleteCommand { get; private set; }
       public ICommand EditCommand { get; private set; }

       private void Save()
       {
           if (this.CurrentTitleName != null)
           {
               Title title = new Title()
               {
                   TitleText = this.CurrentTitleName,
                   TitleGender = this.CurrentTitle.TitleGender

               };
               bool flag = false;
               if (string.IsNullOrEmpty(title.TitleText))
                   return;
               foreach (Title t in this.AllTitles)
               {
                   if (t.TitleText == title.TitleText)
                   {
                       flag = true;
                       break;
                   }
               }
               if (flag)
               {
                   System.Windows.Forms.MessageBox.Show("The Title details are already listed");
                   return;
               }
               titleGateway.SaveNewTitleToDatabase(title.TitleText, title.TitleGender);
               AddNew();
               this.AllTitles = titleGateway.GetAllTitles();
               System.Windows.Forms.MessageBox.Show("The Title details are saved sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please fill Title details");
           }
       }

       private void AddNew()
       {
           this.CurrentTitleName = null;
           this.CurrentGender = null;
       }
       private void Cancel()
       {
           this.CurrentTitle = null;
       }
       private void Delete()
       {
           if (this.CurrentTitle != null)
           {
               titleGateway.DeleteTitleFromDatabase(this.CurrentTitle.TitleID);
               AllTitles.Remove(this.CurrentTitle);
               AddNew();
               System.Windows.Forms.MessageBox.Show("The Title details is deleted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please select Title details to be deleted");
           }

       }

       private void Edit()
       {
           if (this.CurrentTitle != null)
           {
               Title title = new Title()
               {
                   TitleText = this.CurrentTitleName,
                   TitleGender = this.CurrentTitle.TitleGender,
                   TitleID= this.CurrentTitle.TitleID
               };
               titleGateway.EditTitleToDatabase(title.TitleID, title.TitleText, title.TitleGender);
               this.AllTitles = titleGateway.GetAllTitles();
               this.CurrentTitle = title;
               System.Windows.Forms.MessageBox.Show("The Title details is editted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please select Title details to be edited");
           }
       }

       /// <summary>
       /// the TitleName currently selected in the workspace
       /// </summary>
       private string currentTitleName;
       /// <summary>
       /// Gets or sets the TitleName currently selected in the workspace
       /// </summary>
       public string CurrentTitleName
       {
           get
           {
               return this.currentTitleName;
           }

           set
           {
               this.currentTitleName = value;
               this.OnPropertyChanged("CurrentTitleName");
           }
       }

       /// <summary>
       /// The Title currently selected in the workspace
       /// </summary>
       private Title currentTitle;

       /// <summary>
       /// Gets or sets the Title currently selected in the workspace
       /// </summary>
       public Title CurrentTitle
       {
           get
           {
               return this.currentTitle;
           }

           set
           {
               this.currentTitle = value;
               if (this.CurrentTitle != null)
               {
                   this.CurrentTitleName = this.CurrentTitle.TitleText;
                   if (this.CurrentTitle.TitleGender == 0)
                       this.CurrentGender = "Male";
                   if (this.CurrentTitle.TitleGender == 1)
                       this.CurrentGender = "Female";
               }
               
               this.OnPropertyChanged("CurrentTitle");
           }
       }

       /// <summary>
       /// the Gender currently selected in the workspace
       /// </summary>
       private string currentGender;
       /// <summary>
       /// Gets or sets the Gender currently selected in the workspace
       /// </summary>
       public string CurrentGender
       {
           get
           {
               return this.currentGender;
           }

           set
           {
               this.currentGender = value;
               if (CurrentTitle != null)
               {
                   if (value == "Female")
                       this.CurrentTitle.TitleGender = 1;
                   if (value == "Male")
                       this.CurrentTitle.TitleGender = 0;
               }
               this.OnPropertyChanged("CurrentGender");
           }
       }

       /// <summary>
       /// Gets or sets the industry collection
       /// </summary>
       public ObservableCollection<Title> AllTitles
       {
           get
           {
               return this.allTitles;
           }

           set
           {
               this.allTitles = value;
               this.OnPropertyChanged("AllTitles");
           }
       }
    }
}
